<?php


namespace app\controller\admin;


use app\BaseController;
use app\models\config\ArticleModel;
use think\facade\Db;

class ArticleController extends BaseAdminController
{
    function setSearch()
    {
        return [
            'like' => ['title']
        ];
    }

    function setSort()
    {
        return [
        ];
    }

    public function getList()
    {
        try {
            $where = $this->searchWhere;
            $orderBy = $this->orderBy;
            $sort = $this->sort;
            $list = ArticleModel::where($where)
                ->order($orderBy, $sort)
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
//            foreach ($list['data'] as &$v) {
//                $v['cate_name'] = Db::name('category')->where('id', $v['cate_id'])->value('title');
//                $v['type_name'] = Db::name('type')->where('id', $v['type_id'])->value('title');
//            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function getCateList()
    {
        try {
            $list = Db::name('category')->where('id', '>', 0)->select();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function getTypeList()
    {
        try {
            $list = Db::name('type')->where('id', '>', 0)->select();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function add()
    {
        try {
            $data = $this->request->param();
            ArticleModel::create($data);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function edit()
    {
        try {
            $data = $this->request->param();
            ArticleModel::update($data);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function del()
    {
        try {
            $id = $this->request->param('id');
            $info = ArticleModel::find($id);
            $info->delete();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}